package com.space.school.dao.impl;

import com.space.school.dao.ProductInfoDao;
import com.space.school.domain.ProductInfo;
import com.space.school.domain.User;
import com.space.school.utils.SpaceJDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.*;

public class ProductInfoDaoImpl implements ProductInfoDao {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(SpaceJDBCUtils.getDs());
    @Override
    public List<ProductInfo> search(int start, int pageSize, Map<String, String[]> map) {
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("select * from product_info where 1=1 ");
        //        取值的集合
        ArrayList<Object> params=new ArrayList<>();
//        取map中的键的数组
        Set<String> keyset=map.keySet();
        for (String key:keyset) {
            if(!map.get(key)[0].equals("") &&!key.equals("currentPage")){
                stringBuffer.append(" and " + key + " like ?");
                params.add("%"+map.get(key)[0]+"%");
            }
        }
        params.add(start);
        params.add(pageSize);
        stringBuffer.append(" order by product_id  limit ?,? ");
        return jdbcTemplate.query(stringBuffer.toString(), new BeanPropertyRowMapper<ProductInfo>(ProductInfo.class),params.toArray());
    }

    @Override
    public void delete(ProductInfo productInfo) {
        String sql = "delete from product_info where product_id = ?";
        jdbcTemplate.update(sql,productInfo.getProduct_id());
    }

    @Override
    public List<ProductInfo> findById(int id) {
        String sql = "select * from product_info where product_id = ?";
        List<ProductInfo> list=jdbcTemplate.query(sql,new BeanPropertyRowMapper<ProductInfo>(ProductInfo.class),id);
        return list;
    }

    @Override
    public void update(ProductInfo productInfo) {
        String sql = "update product_info set product_name = ?,product_price = ?,product_stock = ?,product_description = ?,category_type = ?,update_time = ? where product_id = ?";
        jdbcTemplate.update(sql,productInfo.getProduct_name(),productInfo.getProduct_price(),productInfo.getProduct_stock(),productInfo.getProduct_description(),productInfo.getCategory_type(),new Date(),productInfo.getProduct_id());
    }

    @Override
    public int count(Map<String, String[]> map) {
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("select count(*) from product_info where 1=1  ");
        //        取值的集合
        ArrayList<Object> params=new ArrayList<>();
//        取map中的键的数组
        Set<String> keyset=map.keySet();
        for (String key:keyset) {
            if(!map.get(key)[0].equals("")&&!key.equals("currentPage")){
                stringBuffer.append(" and " + key + " like ?");
                params.add("%"+map.get(key)[0]+"%");
            }
        }
        return jdbcTemplate.queryForObject(stringBuffer.toString(),Integer.class,params.toArray());
    }

    @Override
    public void updateStatus(int id, int status) {
        String sql = "update product_info set product_status = ? where product_id = ?";
        jdbcTemplate.update(sql,status,id);
    }
}
